//
//  ViewController.m
//  CoreData持久化
//
//  Created by apple on 15/11/20.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"
@interface ViewController ()
@property (nonatomic, strong)CoreDataManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //创建一个数据管理器

    self.manager = [CoreDataManager defaults];
    
    NSLog(@"url ====== %@",[self.manager applicationDocumentsDirectory]);
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//coredata添加方法
- (IBAction)add:(id)sender {
    
       //创建一个实体描述类
    //参数1, 描述的实体
    //参数2, 数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    
    
    
    //创建一个student实体
    //参数1,实体描述类
    //参数2, 数据管理器
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    stu.name = @"根";
    stu.age = @1000;
    stu.sex = @"?";
    stu.hobby = @"吹🐂";
    stu.number = @122333;
    [self.manager saveContext];
    
   
}
//查找
- (IBAction)find:(id)sender {
    
  //首先创建一个查找的请求 相当于数据库里的查询语句
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    //添加一个约束 谓词(只有两种答案,是,不是)
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"name",@"%@"],@"根"];
    //添加约束
    request.predicate = predicate;
    
    //生成的东西存在一个数组里,数组里的东西是乱序的,通过某个KEY先排下顺序
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    
    
    NSError *error;
    //查找到的所有对象
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"%ld",array.count);
    
    
    
}
//修改
- (IBAction)xiugai:(id)sender {
    //修改里先进行查找
    //首先创建一个查找的请求 相当于数据库里的查询语句
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    //添加一个约束 谓词(只有两种答案,是,不是)
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"name",@"%@"],@"根"];
    //添加约束
    request.predicate = predicate;
    
    //生成的东西存在一个数组里,数组里的东西是乱序的,通过某个KEY先排下顺序
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    
    
    NSError *error;
    //查找到的所有对象
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
   

    //修改
    Student *stu = [array lastObject];
    stu.sex = @"女";
    
    
    //还得保存一下
    
    [self.manager saveContext];
    
    
}
//删除
- (IBAction)delete:(id)sender {
    //这些话打fetch回车就都出来了
    
    
    
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    //添加一个约束 谓词(只有两种答案,是,不是)
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"name",@"%@"],@"根"];
    //添加约束
    request.predicate = predicate;
    
    //生成的东西存在一个数组里,数组里的东西是乱序的,通过某个KEY先排下顺序
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    
    
    NSError *error;
    //查找到的所有对象
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    
    
    //删除  实体管理类
    for (Student *stu in array) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    
    //还得保存一下
    
    [self.manager saveContext];
    
    
}




@end
